package com.smartfast4j.manager.modules.sys.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartfast4j.common.Constants;
import com.smartfast4j.common.annotation.LogRecord;
import com.smartfast4j.common.annotation.NotEmpty;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.manager.common.controller.ManagerBaseController;
import com.smartfast4j.manager.modules.sys.service.SysDictService;
import com.smartfast4j.pojo.sys.SysDict;
import com.smartfast4j.pojo.sys.SysDictExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* SysDictController
*  由代码生成器生成
*/
@RestController
@RequestMapping("/sysDict")
public class SysDictController extends ManagerBaseController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 保存
     *
     */
    @LogRecord(operation = "保存数据",remark = "保存系统字典",moduleName = "字典管理")
    @NotEmpty(value = {"dictName","dictValue"},message = {"请输入字典名称","请输入字典的值"})//使用自定义注解进行非空校验
    @RequestMapping("/save")
    public R save(SysDict sysDict) {
         sysDictService.save(sysDict);
         return R.ok();
    }

    /**
     * 根据id批量删除字典
     * @param ids 要删除的字典ID，以逗号分隔
     * @return
     */
    @LogRecord(operation = "删除数据",remark = "根据id批量字典",moduleName = "字典管理")
    @NotEmpty(value = "ids",message = "要删除的字典ID不能为空")//使用自定义注解进行非空校验
    @RequestMapping(value = "/delete")
    public R delete(String ids){
        List<Long> idList = new ArrayList<>();
        String[] idArr = ids.split(",");
        for(String id:idArr){
            idList.add(Long.parseLong(id));
        }
        //根据id删除
        sysDictService.delete(idList);
        return R.ok();
    }

    /**
     * 获取系统字典列表
     * @return
     */
    @RequestMapping(value = "/list")
    public R list(Integer offset, Integer limit, String keyWord) {
        if (offset != null && limit != null) {
            //分页查询
            PageHelper.offsetPage(offset, limit);
        }
        SysDictExample example = new SysDictExample();
        SysDictExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO);//未删除
        if(StringUtils.isNotEmpty(keyWord)){
            criteria.andDictNameLike("%"+keyWord+"%");
            example.or().andDictValueLike("%"+keyWord+"%").andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO);
        }
        example.setOrderByClause("create_time desc");
        //只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页。
        List<SysDict> sysDictList = sysDictService.selectByExample(example);
        PageInfo<SysDict> pageInfo = new PageInfo<>(sysDictList);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total",pageInfo.getTotal());
        dataMap.put("rows", pageInfo.getList());
        //服务端分页，返回的结果必须包含total、rows两个参数。漏写或错写都会导致表格无法显示数据。
        return R.ok(dataMap);
    }

    /**
     * 获取系统字典列表
     * @return
     */
    @RequestMapping(value = "/select")
    public List<SysDict> select(){
        //todo 待优化，先从缓存中取
        SysDictExample example = new SysDictExample();
        SysDictExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO);//未删除
        example.setOrderByClause("create_time desc");
        List<SysDict> sysDictList = sysDictService.selectByExample(example);
        return sysDictList;
    }

     /**
     * 分页查询
     *
     */
    @RequestMapping("/pageList")
    public R pageList(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "10")int pageSize){
        SysDictExample example = new SysDictExample();
        //分页查询
        example.setLimitStart((pageNum-1) * pageSize);//从第几条开始取（索引从0开始）
        example.setLimitEnd(pageSize);//取多少条
        List<SysDict> dataList = sysDictService.selectByExample(example);
        return R.ok(dataList);
    }
}
